/*

 * Copyright (C) 2021 Huawei Device Co., Ltd.

 * Licensed under the Apache License, Version 2.0 (the "License");

 * you may not use this file except in compliance with the License.

 * You may obtain a copy of the License at

 *

 *     http://www.apache.org/licenses/LICENSE-2.0

 *

 * Unless required by applicable law or agreed to in writing, software

 * distributed under the License is distributed on an "AS IS" BASIS,

 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

 * See the License for the specific language governing permissions and

 * limitations under the License.

 */
package com.chinalwb.are.utils;

import com.chinalwb.are.ListStateBean;
import com.chinalwb.are.view.ColorPickerDialog;
import com.chinalwb.are.view.SelectFontSizeDialog;
import ohos.agp.components.ComponentContainer;
import ohos.agp.utils.Color;
import ohos.app.Context;

import java.util.HashMap;

/**
 * 字体样式工具类
 *
 * @since 2021/06/22
 */
public class TextStyleUtils {
    private static TextStyleUtils styleUtils = new TextStyleUtils();
    private final HashMap<Integer, Boolean> styleMap = new HashMap<>();
    private int tempSize = 18;
    private int color = Color.BLACK.getValue();
    private int bgColor = Color.TRANSPARENT.getValue();

    /**
     * 单例
     *
     * @return TextStyleUtils
     */
    public static TextStyleUtils getInstance() {
        return styleUtils;
    }

    /**
     * 字体大小
     *
     * @param context                context
     * @param color                  color
     * @param parent                 parent
     * @param textSize               textSize
     * @param selectTextSizeListener selectTextSizeListener
     */
    public void setTextSize(Context context, String color,
                            ComponentContainer parent, int textSize,
                            SelectFontSizeDialog.SelectTextSizeListener selectTextSizeListener) {
        StateBean stateBean = new StateBean();
        HashMap<Integer, Boolean> tempMap = new HashMap<>();
        SelectFontSizeDialog selectFontSizeDialog = new SelectFontSizeDialog(context, parent, textSize);
        selectFontSizeDialog.show();
        selectFontSizeDialog.setSelectColorListener(size -> {
            TextStyleUtils.this.color = Color.getIntColor(color);
            tempSize = size;
            styleMap.put(TextDecorator.ABSOLUTE_SIZE, true);
            tempMap.putAll(styleMap);
            stateBean.setSize(size);
            stateBean.setColor(Color.getIntColor(color));
            stateBean.setBgColor(bgColor);
            stateBean.setStyleMap(tempMap);
            selectTextSizeListener.onSelectTextSizeListener(tempSize);
            TextDecorator.getInstance().setStateBean(stateBean);
        });
    }

    /**
     * 设置粗体
     *
     * @param listStateBean listStateBean
     */
    public void setTextBold(ListStateBean listStateBean) {
        StateBean stateBean = new StateBean();
        stateBean.setColor(color);
        stateBean.setSize(tempSize);
        stateBean.setBgColor(bgColor);
        HashMap<Integer, Boolean> tempMap = new HashMap<>();
        setStyle(listStateBean, TextDecorator.TYPE_BOLD, stateBean, tempMap);
        TextDecorator.getInstance().setStateBean(stateBean);
    }

    /**
     * 斜体
     *
     * @param listStateBean listStateBean
     */
    public void setTextItalic(ListStateBean listStateBean) {
        StateBean stateBean = new StateBean();
        stateBean.setColor(color);
        stateBean.setSize(tempSize);
        stateBean.setBgColor(bgColor);
        HashMap<Integer, Boolean> tempMap = new HashMap<>();
        setStyle(listStateBean, TextDecorator.TYPE_ITALIC, stateBean, tempMap);
        TextDecorator.getInstance().setStateBean(stateBean);
    }

    /**
     * 下划线
     *
     * @param listStateBean listStateBean
     */
    public void setTextUnderLine(ListStateBean listStateBean) {
        StateBean stateBean = new StateBean();
        stateBean.setColor(color);
        stateBean.setSize(tempSize);
        stateBean.setBgColor(bgColor);
        HashMap<Integer, Boolean> tempMap = new HashMap<>();
        setStyle(listStateBean, TextDecorator.UNDERLINE, stateBean, tempMap);
        TextDecorator.getInstance().setStateBean(stateBean);
    }

    /**
     * 上标
     *
     * @param listStateBean listStateBean
     */
    public void setTextSuperscript(ListStateBean listStateBean) {
        StateBean stateBean = new StateBean();
        stateBean.setColor(color);
        stateBean.setSize(tempSize);
        stateBean.setBgColor(bgColor);
        HashMap<Integer, Boolean> tempMap = new HashMap<>();
        setStyle(listStateBean, TextDecorator.SUPERSCRIPT, stateBean, tempMap);
        TextDecorator.getInstance().setStateBean(stateBean);
    }

    /**
     * 下标
     *
     * @param listStateBean listStateBean
     */
    public void setTextSubscript(ListStateBean listStateBean) {
        StateBean stateBean = new StateBean();
        stateBean.setColor(color);
        stateBean.setSize(tempSize);
        stateBean.setBgColor(bgColor);
        HashMap<Integer, Boolean> tempMap = new HashMap<>();
        setStyle(listStateBean, TextDecorator.SUBSCRIPT, stateBean, tempMap);
        TextDecorator.getInstance().setStateBean(stateBean);
    }

    /**
     * 中划线
     *
     * @param listStateBean listStateBean
     */
    public void setTextStrikeThrough(ListStateBean listStateBean) {
        StateBean stateBean = new StateBean();
        stateBean.setColor(color);
        stateBean.setSize(tempSize);
        stateBean.setBgColor(bgColor);
        HashMap<Integer, Boolean> tempMap = new HashMap<>();
        setStyle(listStateBean, TextDecorator.STRIKETHROUGH, stateBean, tempMap);
        TextDecorator.getInstance().setStateBean(stateBean);
    }

    /**
     * 缩小
     *
     * @param listStateBean listStateBean
     * @param textSize      textSize
     * @return int
     */
    public int setTextMinify(ListStateBean listStateBean, int textSize) {
        StateBean stateBean = new StateBean();
        stateBean.setColor(color);
        HashMap<Integer, Boolean> tempMap = new HashMap<>();
        setStyle(listStateBean, TextDecorator.TEXT_MINIFY, stateBean, tempMap);
        textSize = listStateBean.getIsSelected() ? textSize / 2 : textSize * 2;
        if (textSize < 12) {
            textSize = 12;
        }
        if (textSize > 50) {
            textSize = 50;
        }
        stateBean.setSize(textSize);
        TextDecorator.getInstance().setStateBean(stateBean);
        this.tempSize = textSize;
        return textSize;
    }

    /**
     * 放大
     *
     * @param listStateBean listStateBean
     * @param textSize      textSize
     * @return int
     */
    public int setTextScale(ListStateBean listStateBean, int textSize) {
        StateBean stateBean = new StateBean();
        stateBean.setColor(color);
        stateBean.setBgColor(bgColor);
        HashMap<Integer, Boolean> tempMap = new HashMap<>();
        setStyle(listStateBean, TextDecorator.TEXT_SCALE, stateBean, tempMap);
        textSize = listStateBean.getIsSelected() ? textSize * 2 : textSize / 2;
        if (textSize > 50) {
            textSize = 50;
        }
        if (textSize < 12) {
            textSize = 12;
        }
        stateBean.setSize(textSize);
        TextDecorator.getInstance().setStateBean(stateBean);
        this.tempSize = textSize;
        return textSize;
    }

    /**
     * 背景
     *
     * @param listStateBean listStateBean
     */
    public void setTextBg(ListStateBean listStateBean) {
        StateBean stateBean = new StateBean();
        stateBean.setColor(color);
        stateBean.setSize(tempSize);
        stateBean.setBgColor(bgColor);
        HashMap<Integer, Boolean> tempMap = new HashMap<>();
        setStyle(listStateBean, TextDecorator.TEXT_BAG, stateBean, tempMap);
        bgColor = listStateBean.getIsSelected() ? Color.YELLOW.getValue() : Color.TRANSPARENT.getValue();
        stateBean.setBgColor(bgColor);
        TextDecorator.getInstance().setStateBean(stateBean);
    }

    /**
     * 颜色
     *
     * @param context             context
     * @param textSize            textSize
     * @param parent              parent
     * @param color               color
     * @param selectColorListener selectColorListener
     */
    public void setTextColor(Context context, int textSize, ComponentContainer parent,
                             String color, ColorPickerDialog.SelectColorListener selectColorListener) {
        StateBean stateBean = new StateBean();
        ColorPickerDialog colorPickerDialog = new ColorPickerDialog(context, parent, color);
        colorPickerDialog.show();
        colorPickerDialog.setSelectColorListener(colors -> {
            TextStyleUtils.this.color = Color.getIntColor(colors);
            styleMap.put(TextDecorator.TEXT_COLOR, true);
            stateBean.setColor(Color.getIntColor(colors));
            stateBean.setSize(textSize);
            stateBean.setStyleMap(styleMap);
            stateBean.setBgColor(bgColor);
            selectColorListener.onSelectColorListener(colors);
            TextDecorator.getInstance().setStateBean(stateBean);
        });
    }

    /**
     * 样式
     *
     * @param listStateBean listStateBean
     * @param style         style
     * @param stateBean     stateBean
     * @param tempMap       tempMap
     */
    private void setStyle(ListStateBean listStateBean, int style,
                          StateBean stateBean, HashMap<Integer, Boolean> tempMap) {
        if (listStateBean.getIsSelected()) {
            styleMap.put(style, true);
        } else {
            styleMap.remove(style);
        }
        tempMap.putAll(styleMap);
        stateBean.setStyleMap(tempMap);
    }

    /**
     * 清空数据
     */
    public void release() {
        styleMap.clear();
    }

}
